// 获取相差的天数和小时
const DifferenceDaysandHours = (timestamp1, timestamp2) => {
	let diffMs = timestamp2 - timestamp1; // 相差的毫秒数 
	let diffDays = Math.floor(diffMs / (24 * 60 * 60 * 1000));
	// 相差的天数，向下取整 
	let diffHours = Math.floor((diffMs - diffDays * 24 * 60 * 60 * 1000) / (60 * 60 * 1000));
	// 相差的小时数，向下取整 
	let TimeDifference = {}
	// 对象赋值
	TimeDifference.diffDays = diffDays
	TimeDifference.diffHours = diffHours
	return TimeDifference
}

// 时间戳获取时分
const setTimeDivision = (TIME) => {
	return dayjs(TIME).format('HH:mm');
}
const setAes = ()=>{
	return false
}
// 获取token
const uniGetToken = () => {
	try {
		const value = uni.getStorageSync('accessToken');
		if (value) {
			return value
		} else {
			return ''
		}
	} catch (e) {
		// error
	}
}

// 时分秒转换
const calculateDiffTime = (startTime, endTime) => {

	let runTime = parseInt((endTime - startTime) / 1000);
	let year = Math.floor(runTime / 86400 / 365);
	runTime = runTime % (86400 * 365);
	let month = Math.floor(runTime / 86400 / 30);
	runTime = runTime % (86400 * 30);
	let day = Math.floor(runTime / 86400);
	runTime = runTime % 86400;
	let hour = Math.floor(runTime / 3600);
	runTime = runTime % 3600;
	let minute = Math.floor(runTime / 60);
	runTime = runTime % 60;
	let second = runTime;
	let result = {
		year,
		month,
		day,
		hour,
		minute,
		second
	};
	// console.log(`${hour}小时${minute}分${second}秒`);
	let res = `${hour}小时${minute}分${second}秒`
	return res;
};

const yearCalculateDiffTime = (startTime, endTime) => {
	let runTime = parseInt((endTime - startTime) / 1000);
	let year = Math.floor(runTime / 86400 / 365);
	runTime = runTime % (86400 * 365);
	let month = Math.floor(runTime / 86400 / 30);
	runTime = runTime % (86400 * 30);
	let day = Math.floor(runTime / 86400);
	runTime = runTime % 86400;
	let hour = Math.floor(runTime / 3600);
	runTime = runTime % 3600;
	let minute = Math.floor(runTime / 60);
	runTime = runTime % 60;
	let second = runTime;
	let result = {
		year,
		month,
		day,
		hour,
		minute,
		second
	};


	let res = `${day}天${hour}小时${minute}分${second}秒`
	return res;
};


// 重组数据
export function setRecombination(arr) {
	let list = [];

	arr.forEach(item => {
		if (item.id) {
			// 如果是父节点，创建一个新对象并初始化子节点为空数组
			list.push({
				id: item.id,
				name: item.name,
				child: []
			});
		}
	});

	arr.forEach(item => {
		if (item.parentId) {
			// 如果是子节点，找到对应的父节点并将其添加到父节点的子节点数组中
			let parent = list.find(parent => parent.id === item.parentId);
			parent.child.push({
				parentId: item.parentId,
				name: item.name
			});
		}
	});
	return list
}

// 重构二级联动数据给uview2 Picker使用
export function constructGroups(arr) {
	let columns = [
		[],
		[]
	];
	let groupById = {};

	// 初始化 groupById
	arr.forEach(item => {
		if (item.id) {
			groupById[item.id] = [item.name]; // 初始化数组并添加id元素的name
		}
	});

	// 根据 parentId 填充 parentId
	arr.forEach(item => {
		if (item.parentId && groupById[item.parentId]) {
			groupById[item.parentId].push(item.name); // 将子元素name加入对应的父id组
		}
	});

	// 从 groupById 中构建 columnData
	let columnData = Object.values(groupById).map(group => group.slice(1)); // 去掉第一个元素

	// 构建 columns 数组
	arr.forEach(item => {
		if (item.id && !columns[0].includes(item.name)) {
			columns[0].push(item.name); // 第一列添加 id 对应的 name
		}
	});

	// 构建第二列数据
	arr.forEach(item => {
		if (columns[0].includes(item.name)) {
			let id = item.id;
			if (groupById[id]) {
				groupById[id].slice(1).forEach(name => { // 只取第二列数据
					if (!columns[1].includes(name)) {
						columns[1].push(name);
					}
				});
			}
		}
	});
	return {
		columns,
		columnData
	};
}
// 时间戳转 xxxx-xx-xx格式
export function formatTimestamp(timestamp) {
	const date = new Date(timestamp);

	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以要加1
	const day = String(date.getDate()).padStart(2, '0');

	return `${year}-${month}-${day}`;
}
export function setTimes(timestamp) {
	const date = new Date(timestamp);

	// 格式化为 yyyy-mm-dd hh:mm:ss
	return (
		date.getFullYear() +
		'-' +
		String(date.getMonth() + 1).padStart(2, '0') +
		'-' +
		String(date.getDate()).padStart(2, '0') +
		' ' +
		String(date.getHours()).padStart(2, '0') + // 小时
		':' +
		String(date.getMinutes()).padStart(2, '0') + // 分钟
		':' +
		String(date.getSeconds()).padStart(2, '0') // 秒
	);
}
export function navigateToPage(url, delay = 10000) {
	let isNavigating = false;
	console.log('isNavigating:', isNavigating);
	return function() {
		if (isNavigating) return; // 防止重复点击

		isNavigating = true; // 开始跳转

		// 执行跳转
		uni.navigateTo({
			url: url
		});

		// 延迟恢复状态
		setTimeout(() => {
			isNavigating = false;
		}, delay); // 延迟时间可以灵活设置
	};
}

export function Toast(title) {
	uni.showToast({
		title: title,
		icon: 'none'
	})
}

export {
	DifferenceDaysandHours,
	setTimeDivision,
	calculateDiffTime,
	yearCalculateDiffTime,
	uniGetToken,
	setAes,
}


//跳转页面
export function jump(url) {
	if (url) {
		uni.navigateTo({
			url: url
		});
	} else {
		uni.showToast({
			icon: 'none',
			title: '敬请期待'
		})
	}
}

//返回页面
export function back() {
	uni.navigateBack()
}

//复制文字
export function copy(value) {
	uni.setClipboardData({
		data: value + '', //要被复制的内容
		success: () => { //复制成功的回调函数
			uni.showToast({ //提示
				title: '复制成功'
			})
		}
	});
}

